package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.ConfigPayway;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IConfigPaywayService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ConfigPaywayVO;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 支付方维护：只新增、修改、查询，无删除 前端控制器
 * </p>
 *
 * @author xuming
 * @since 2021-06-25
 */
@RestController
@RequestMapping("/configPayway")
@Slf4j
public class ConfigPaywayController extends BaseController {

    @Resource
    private IConfigPaywayService iConfigPaywayService;

    @Resource
    private IUsersService iUsersService;

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 添加支付方维护
     *
     * @param configPayway
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加支付方维护", notes = "添加支付方维护")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configPayway:create")
    @RequestMapping(value = "/addConfigPayway", method = RequestMethod.POST)
    public ResponseBean addConfigPayway(@RequestBody ConfigPayway configPayway) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Users user = iUsersService.getUserByName((String) obj);

        configPayway.setOperateTime(new Date());
        configPayway.setOperateUser(user.getUserName());


        ResponseMessageVO responseMessageVO = iConfigPaywayService.addConfigPayway(configPayway);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 查询支付方维护
     *
     * @param configPaywayVO
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询支付方维护", notes = "查询支付方维护")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configPayway:view")
    @RequestMapping(value = "/pageConfigPayway", method = RequestMethod.POST)
    public ResponseBean pageConfigPayway(@RequestBody ConfigPaywayVO configPaywayVO,
                                              @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
                                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) throws Exception {


        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        //分页查询全部记录
        if (pageSize == 0) {
            pageSize = Integer.MAX_VALUE;
        }

        IPage<ConfigPayway> page = new Page(currentPage, pageSize);
        QueryWrapper<ConfigPayway> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(ConfigPayway.ID,ConfigPayway.NAME,ConfigPayway.OPERATE_TIME,ConfigPayway.OPERATE_USER);

        if (Utils.notEmpty(configPaywayVO.getType())) {
            queryWrapper.eq(ConfigPayway.TYPE, configPaywayVO.getType());
        }

        if (Utils.notEmpty(configPaywayVO.getName())) {
            queryWrapper.like(ConfigPayway.NAME, configPaywayVO.getName());
        }

        if (Utils.notEmpty(configPaywayVO.getOperateTimeStart())) {
            // 四位年-两位月-两位日 两位小时：两位分钟：两位秒
            queryWrapper.ge(ConfigPayway.OPERATE_TIME, sdf.parse(configPaywayVO.getOperateTimeStart()));
        }
        if (Utils.notEmpty(configPaywayVO.getOperateTimeEnd())) {
            queryWrapper.le(ConfigPayway.OPERATE_TIME, sdf.parse(configPaywayVO.getOperateTimeEnd()));
        }
        queryWrapper.orderByDesc(ConfigPayway.OPERATE_TIME);

        page = iConfigPaywayService.page(page, queryWrapper);
        return new ResponseBean(StatusCode.OK, page);
    }

    /**
     * 修改支付方维护
     *
     * @param configPayway
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改支付方维护", notes = "修改支付方维护")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configPayway:edit")
    @RequestMapping(value = "/updateConfigPayway", method = RequestMethod.POST)
    public ResponseBean updateConfigPayway(@RequestBody ConfigPayway configPayway) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        // 当前登录人的获取
        Users user = iUsersService.getUserByName((String) obj);
        configPayway.setOperateTime(new Date());
        configPayway.setOperateUser(user.getUserName());

        ResponseMessageVO responseMessageVO = iConfigPaywayService.updateConfigPayway(configPayway);
        this.writeLog2Manager(responseMessageVO);
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }
}
